package com.ssyc.gsk_parents.activity;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.StarBar;
import com.ssyc.common.view.ZzHorizontalProgressBar;
import com.ssyc.gsk_parents.R;
import com.ssyc.gsk_parents.adapter.EvaluateRvAdapter;
import com.ssyc.gsk_parents.bean.EvaluateInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 评价老师
 * Created by Administrator on 2018/6/21 0021.
 */

public class ParentsEvaluateTeacherActivity extends BaseActivity implements View.OnClickListener, SrfManager.OnPullRefreshListener {

    private ImageView ivBack;
    private ImageView ivEdit;
    private RecyclerView rv;
    private SmartRefreshLayout srf;
    private RelativeLayout rlLoadingView;
    //界面刷新的标识
    public static final int EVALUATEUPDATE = 0x550;

    //数据集合
    private List<EvaluateInfo.DataBean.PeComtentBean> oldDatas;

    //适配器
    private EvaluateRvAdapter adapter;


    //头布局
    private View headView;

    //空视图
    private View emptyView;


    //评分控件
    private StarBar starBar;
    //进度条控件和显示进度的文字
    private ZzHorizontalProgressBar pbFive, pbFour, pbThree, pbTwo, pbOne;
    private TextView tvFive, tvFour, tvThree, tvTwo, tvOne, Score;


    private String infoJson;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_evaluate_teacher;
    }

    @Override
    protected void init() {
        showContent();

        //初始化视图
        initView();

        //初始化下拉刷新控件
        initSrf();

        //设置数据
        initRv();

        //请求数据
        http(BqaManager.NORML);
    }


    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        srf = findViewById(R.id.srf);
        rlLoadingView = findViewById(R.id.rl_loading_view);
        rlLoadingView.setVisibility(View.VISIBLE);
        ivEdit = findViewById(R.id.iv_edit);
        ivEdit.setOnClickListener(this);

        headView = View.inflate(this, R.layout.parent_headview_evaluate, null);
        starBar = headView.findViewById(R.id.starbar);
        starBar.setEnabled(false);
        pbOne = headView.findViewById(R.id.pb_one);
        pbTwo = headView.findViewById(R.id.pb_two);
        pbThree = headView.findViewById(R.id.pb_three);
        pbFour = headView.findViewById(R.id.pb_four);
        pbFive = headView.findViewById(R.id.pb_five);
        tvOne = headView.findViewById(R.id.tv_one);
        tvTwo = headView.findViewById(R.id.tv_two);
        tvThree = headView.findViewById(R.id.tv_three);
        tvFour = headView.findViewById(R.id.tv_four);
        tvFive = headView.findViewById(R.id.tv_five);
        Score = headView.findViewById(R.id.tv_show_score);
    }


    /**
     * 初始化下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 请求服务器数据的方法
     *
     * @param method
     */
    private void http(int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "29");
        map.put("acc", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("apptoken", AccountUtils.getToken(this));
        map.put("platform", "8");
        HttpUtils.post(HttpAdress.EvaluateRecord, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {

                if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (!TextUtils.isEmpty(response)) {
                    EvaluateInfo info = null;
                    String json = response;
                    try {
                        info = GsonUtil.jsonToBean(json, EvaluateInfo.class);
                    } catch (Exception e) {
                        Log.i("test", "数据解析异常");
                    }
                    Log.i("test", "评价列表：" + response);

                    if (null != info) {
                        if ("200".equals(info.getState())) {

                            //获取到数据后初始化头部数据
                            initHeadViewData(info.getData());

                            List<EvaluateInfo.DataBean.PeComtentBean> newDatas = new ArrayList<>();
                            newDatas = info.getData().getPe_comtent();
                            //倒序处理
                            Collections.reverse(newDatas);
                            infoJson = json;

                            oldDatas.clear();
                            oldDatas.addAll(newDatas);


                            if (method == BqaManager.NORML) {

                                BqaManager.setRv(emptyView, ParentsEvaluateTeacherActivity.this,
                                        adapter, rv, null);
                            } else {
                                oldDatas.clear();
                                //更新数据
                                BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                            }
                            ivEdit.setVisibility(View.VISIBLE);
                        } else if ("10043".equals(info.getState())) {
                            UiUtils.Toast("账号无对应的教师信息", false);
                            BqaManager.setRv(emptyView, ParentsEvaluateTeacherActivity.this,
                                    adapter, rv, null);
                            ivEdit.setVisibility(View.GONE);
                            //空数据头部数据
                            initHeadViewEmptyData();
                        } else {
                            Log.i("test", "错误,状态码是:" + info.getState());
                            ivEdit.setVisibility(View.VISIBLE);
                            BqaManager.setRv(emptyView, ParentsEvaluateTeacherActivity.this,
                                    adapter, rv, null);
                            ivEdit.setVisibility(View.GONE);
                            //空数据头部数据
                            initHeadViewEmptyData();
                        }
                    } else {
                        Log.i("test", "返回数据为空");
                    }
                } else {
                    Log.i("test", "返回数据为空");
                }
            }
        });
    }


    /**
     * 设置列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view_top, null);
        TextView tvMessage = emptyView.findViewById(R.id.tv_content);
        tvMessage.setText("暂无评价记录");
        adapter = new EvaluateRvAdapter(this, R.layout.parent_rv_evaluate_teacher, oldDatas);
        adapter.addHeaderView(headView);
    }


    /**
     * 初始化头部数据
     */
    private void initHeadViewData(EvaluateInfo.DataBean dataBean) {

        //设置评分控件的值
        starBar.setStarMark(Float.parseFloat(dataBean.getAverage()));

        //设置评分值
        Score.setText(dataBean.getAverage());

        //设置几个进度条的值
        //set progress value
        pbFive.setProgress((int) Float.parseFloat(dataBean.getFive_star()));
        pbFour.setProgress((int) Float.parseFloat(dataBean.getFour_star()));
        pbThree.setProgress((int) Float.parseFloat(dataBean.getThree_star()));
        pbTwo.setProgress((int) Float.parseFloat(dataBean.getTwo_star()));
        pbOne.setProgress((int) Float.parseFloat(dataBean.getOne_star()));

        if("100".equals(dataBean.getFive_star())){
            tvFive.setText(dataBean.getFive_star() + ".00%");
            tvFour.setText(dataBean.getFour_star()+ ".00%");
            tvThree.setText(dataBean.getThree_star() + ".00%");
            tvTwo.setText(dataBean.getTwo_star() + ".00%");
            tvOne.setText(dataBean.getOne_star() + ".00%");
        }else{
            tvFive.setText(dataBean.getFive_star() + "%");
            tvFour.setText(dataBean.getFour_star()+ "%");
            tvThree.setText(dataBean.getThree_star() + "%");
            tvTwo.setText(dataBean.getTwo_star() + "%");
            tvOne.setText(dataBean.getOne_star() + "%");
        }

        //set padding
        pbFive.setPadding(0);
        pbFour.setPadding(0);
        pbThree.setPadding(0);
        pbTwo.setPadding(0);
        pbOne.setPadding(0);

        //set bacground color
        pbFive.setBgColor(getResources().getColor(R.color.parent_pb_bg));
        pbFour.setBgColor(getResources().getColor(R.color.parent_pb_bg));
        pbThree.setBgColor(getResources().getColor(R.color.parent_pb_bg));
        pbTwo.setBgColor(getResources().getColor(R.color.parent_pb_bg));
        pbOne.setBgColor(getResources().getColor(R.color.parent_pb_bg));

        //set progress color
        pbFive.setProgressColor(getResources().getColor(R.color.parent_pb_progress));
        pbFour.setProgressColor(getResources().getColor(R.color.parent_pb_progress));
        pbThree.setProgressColor(getResources().getColor(R.color.parent_pb_progress));
        pbTwo.setProgressColor(getResources().getColor(R.color.parent_pb_progress));
        pbOne.setProgressColor(getResources().getColor(R.color.parent_pb_progress));

    }

    /**
     * 初始化头部数据
     */
    private void initHeadViewEmptyData() {

        //设置评分控件的值
        starBar.setStarMark((float) 5.00);

        //设置评分值
        Score.setText("5.00");

        //设置几个进度条的值
        //set progress value
        pbFive.setProgress(5);
        pbFour.setProgress(0);
        pbThree.setProgress(0);
        pbTwo.setProgress(0);
        pbOne.setProgress(0);

            tvFive.setText("100.00%");
            tvFour.setText("0.00%");
            tvThree.setText("0.00%");
            tvTwo.setText("0.00%");
            tvOne.setText("0.00%");

        //set padding
        pbFive.setPadding(0);
        pbFour.setPadding(0);
        pbThree.setPadding(0);
        pbTwo.setPadding(0);
        pbOne.setPadding(0);

        //set bacground color
        pbFive.setBgColor(getResources().getColor(R.color.parent_pb_bg));
        pbFour.setBgColor(getResources().getColor(R.color.parent_pb_bg));
        pbThree.setBgColor(getResources().getColor(R.color.parent_pb_bg));
        pbTwo.setBgColor(getResources().getColor(R.color.parent_pb_bg));
        pbOne.setBgColor(getResources().getColor(R.color.parent_pb_bg));

        //set progress color
        pbFive.setProgressColor(getResources().getColor(R.color.parent_pb_progress));
        pbFour.setProgressColor(getResources().getColor(R.color.parent_pb_progress));
        pbThree.setProgressColor(getResources().getColor(R.color.parent_pb_progress));
        pbTwo.setProgressColor(getResources().getColor(R.color.parent_pb_progress));
        pbOne.setProgressColor(getResources().getColor(R.color.parent_pb_progress));

    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }


    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.iv_edit) {
            //添加评价

            Intent intent = new Intent(this, ParentsAddNewEvaluateActiivty.class);
            intent.putExtra("infoJson", infoJson);
            startActivity(intent);
        }
    }


    /**
     * 事件
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        //更新界面的消息
        if (null != busInfo && busInfo.getType() == EVALUATEUPDATE) {
            http(BqaManager.PULLREFRESH);
        }
    }


}
